/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package uk.surrey.ash.ga;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * @author Ashish
 *
 */
public class Population {
	private ArrayList<Chromosome> population = new ArrayList<Chromosome>();

	/**
	 * @param cellspace the population to set
	 */
	public void setPopulation(CellSpace cellspace) {
//		Initial population will be a projection to two bins
		int numberOfChromosomes = 16;
		int numberOfGenes = 128;
		for(int i=0;i<numberOfChromosomes;i++){
			Chromosome chromosome = new Chromosome(numberOfGenes, cellspace);
			population.add(chromosome);
		}
	}

	/**
	 * @return the population
	 */
	public ArrayList<Chromosome> getPopulation() {
		return population;
	}

	/**
	 * 
	 */
	public void mutatePopulation() {
//		select top 50% of chromosomes
		Collections.sort(this.population, geneticFitness);
//		one point cross over mutation
		ArrayList<Chromosome> mutantPopulation = new ArrayList<Chromosome>(this.population.size());
		for(int i=0;i<this.population.size();i++){
			Chromosome chromosome = new Chromosome(this.population,i);	
			mutantPopulation.add(chromosome);
		}
		this.population = mutantPopulation;
	}
	
	static final Comparator<Chromosome> geneticFitness = new Comparator<Chromosome>(){
		public int compare(Chromosome c1, Chromosome c2){
			Double c1geneticFitness = c1.getCodebook().getCodebookFitness();
			Double c2geneticFitness = c2.getCodebook().getCodebookFitness();
			return c1geneticFitness.compareTo(c2geneticFitness);
		}
	};

}
